﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.Sim.Admin.AdminSpecificPermissionRecord;
using Dapper;

namespace BCData.Sim.Admin.AdminSpecificPermissionRecord
{

    public class AdminSpecificPermissionRecordData: IAdminSpecificPermissionRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT AdminSpecificPermissionRecordId,SpecificPermissionKey,SpecificPermissionName,AdminId,StaffOrganizationMappingRecordId,CreateTime FROM AdminSpecificPermissionRecord";
        public AdminSpecificPermissionRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<AdminSpecificPermissionRecordEntity> Add(AdminSpecificPermissionRecordEntity adminSpecificPermissionRecordEntity)
        {
            string sql = SqlCommands.GetInsertSql("AdminSpecificPermissionRecord", new string[] { "SpecificPermissionName", "SpecificPermissionKey", "AdminId", "StaffOrganizationMappingRecordId" });
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SpecificPermissionKey", adminSpecificPermissionRecordEntity.SpecificPermissionKey, DbType.AnsiString, size: 50);
            parameters.Add("?SpecificPermissionName", adminSpecificPermissionRecordEntity.SpecificPermissionName, DbType.AnsiString, size: 50);
            parameters.Add("?AdminId", adminSpecificPermissionRecordEntity.AdminId, DbType.Int64, size: 20);
            parameters.Add("?StaffOrganizationMappingRecordId", adminSpecificPermissionRecordEntity.StaffOrganizationMappingRecordId, DbType.Int64, size: 20);
            sql = string.Format("{0} {1} WHERE AdminSpecificPermissionRecordId=LAST_INSERT_ID()", sql, QUERY);

            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<AdminSpecificPermissionRecordEntity>(sql, parameters, databaseContext.Transaction);
        }

        public void Delete(int adminSpecificPermissionRecordId)
        {
            string sql = @"DELETE FROM AdminSpecificPermissionRecord WHERE AdminSpecificPermissionRecordId=?AdminSpecificPermissionRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AdminSpecificPermissionRecordId", adminSpecificPermissionRecordId, DbType.Int64, size: 20);
            this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction);
        }

        public async Task<IEnumerable<AdminSpecificPermissionRecordEntity>> Query(long adminId)
        {
            string sql = string.Format("{0} WHERE AdminId=?AdminId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AdminId", adminId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<AdminSpecificPermissionRecordEntity>(sql, parameters);
        }

        public async Task<bool> CheckExists(int adminSpecificPermissionRecordId)
        {
            string sql = @"SELECT 1 FROM AdminSpecificPermissionRecord WHERE AdminSpecificPermissionRecordId=?AdminSpecificPermissionRecordId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AdminSpecificPermissionRecordId", adminSpecificPermissionRecordId, DbType.Int64, size: 20);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        
        public async Task<bool> CheckExists(long adminId, string extensionPermissionKey)
        {
            string sql = @"SELECT 1 FROM AdminSpecificPermissionRecord WHERE SpecificPermissionKey=?SpecificPermissionKey AND AdminId=?AdminId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SpecificPermissionKey", extensionPermissionKey, DbType.AnsiString, size: 50);
            parameters.Add("?AdminId", adminId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public void Delete(long adminId, string extensionPermissionKey)
        {
            string sql = @"DELETE FROM AdminSpecificPermissionRecord WHERE SpecificPermissionKey=?SpecificPermissionKey AND AdminId=?AdminId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?SpecificPermissionKey", extensionPermissionKey, DbType.AnsiString, size: 50);
            parameters.Add("?AdminId", adminId, DbType.Int64, size: 20);
            this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction);
        }
    }
}
